$(document).ready(function () {
    Highcharts.setOptions({
        global : {
            useUTC : false
        }
    });

    var chart = Highcharts.stockChart('container', {
        rangeSelector: {
            buttons: [{
                count: 1,
                type: 'minute',
                text: '1M'
            }, {
                count: 5,
                type: 'minute',
                text: '5M'
            }, {
                type: 'all',
                text: 'All'
            }],
            inputEnabled: false,
            selected: 2
        },
        title : {
            text : 'CPU Data'
        },
        tooltip: {
            split: false
        },
        exporting: {
            enabled: true
        },
        xAxis: {
            type: 'datetime',
            labels: {
                format: '{value: %Y-%m-%d %H:%M:%S}',
                align: 'right',
            }
        },
        series : [{
            name : '人流数据',
            data : (function () {
                // generate an array of random data
                var data = [], time = (new Date()).getTime(), i;
                for (i = -12; i <= 0; i += 1) {
                    data.push([
                        time + i * 1000,
                        Math.round(Math.random() * 100)
                    ]);
                }
                return data;
            }())
        },
        {
            name : '流量数据',
            data : (function () {
                // generate an array of random data
                var data = [], time = (new Date()).getTime(), i;
                for (i = -12; i <= 0; i += 1) {
                    data.push([
                        time + i * 1000,
                        Math.round(Math.random() * 100)
                    ]);
                }
                return data;
            }())
        }]
    });

    function getForm(){
        $.ajax({
            url:"http://172.20.12.14:8889/data",
            dataType:'jsonp',
            data:'',
            jsonp:'callback',
            success:function(json) {
                var series = [chart.series[0],chart.series[1]];
                var array = [];
                var array1 = [];
                var time = [];
                json.forEach(function (t) {
                    time=t.time.substring(0,25).replace(/-/g,'/');
                    array.unshift([new Date(time).getTime(),Number(t.mem_usage)]);
                    array1.unshift([new Date(time).getTime(),Number(t.mem_free)]);
                });
                chart.series[0].setData(array);
                chart.series[1].setData(array1);
            }
        })
    }

    $(document).ready(function() {
        //每隔3秒自动调用方法，实现图表的实时更新
        window.setInterval(getForm,3000);
    });

});